<template>
  <div class="center-table-container">
    <!-- 表格头部操作区 -->
    
    
    <!-- 数据表格 -->
    <div class="table-wrapper">
      <el-table 
        :data="filteredData" 
        class="data-table"
        style="width: 100%"
        :header-cell-style="{ background: 'rgba(0, 30, 60, 0.9)', color: '#00d4ff', fontSize: '14px', fontWeight: '600', borderBottom: '2px solid rgba(124, 189, 207, 0.3)' }"
        :row-style="{ background: 'rgba(0, 20, 40, 0.8)', color: '#ffffff' }"
        :cell-style="{ borderBottom: '1px solid rgba(124, 189, 207, 0.2)', fontSize: '13px' }"
      >
        <el-table-column prop="deviceName" label="设备名称" width="180">
          <template #default="{ row }">
            <div class="device-info">
              <div class="device-icon" :class="row.status"></div>
              <span>{{ row.deviceName }}</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column prop="region" label="所在地区" width="120" />
        
        <el-table-column prop="capacity" label="装机容量(MW)" width="130">
          <template #default="{ row }">
            <span class="capacity">{{ row.capacity }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="dailyOutput" label="当日发电量(kWh)" width="150">
          <template #default="{ row }">
            <span class="power-output value">{{ row.dailyOutput.toLocaleString() }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="efficiency" label="发电效率(%)" width="120">
          <template #default="{ row }">
            <div class="efficiency-bar">
              <div class="efficiency-fill" :style="{ width: row.efficiency + '%' }"></div>
              <span class="efficiency-text">{{ row.efficiency }}%</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column prop="status" label="运行状态" width="100">
          <template #default="{ row }">
            <span class="status-badge" :class="row.status">
              {{ row.status === 'online' ? '在线' : '离线' }}
            </span>
          </template>
        </el-table-column>
        
        <el-table-column prop="lastUpdate" label="最后更新" width="140">
          <template #default="{ row }">
            <span class="update-time">{{ row.lastUpdate }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 分页器 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        :page-size="pageSize"
        :total="totalFilteredCount"
        layout="prev, pager, next, jumper, total"
        :pager-count="5"
        :small="false"
        background
        @current-change="changePage"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, reactive } from 'vue'

const searchKeyword = ref('')
const activeFilter = ref('all')
const currentPage = ref(1)
const pageSize = 8

// 模拟表格数据
const tableData = reactive([
  {
    deviceName: '庆云光伏电站A',
    region: '山东德州',
    capacity: 50.5,
    dailyOutput: 125600,
    efficiency: 94.2,
    status: 'online',
    lastUpdate: '2024-01-15 14:30'
  },
  {
    deviceName: '庆云光伏电站B',
    region: '山东德州',
    capacity: 42.8,
    dailyOutput: 98400,
    efficiency: 91.8,
    status: 'online',
    lastUpdate: '2024-01-15 14:28'
  },
  {
    deviceName: '华北风电场1号',
    region: '河北张家口',
    capacity: 75.0,
    dailyOutput: 156800,
    efficiency: 88.5,
    status: 'offline',
    lastUpdate: '2024-01-15 12:45'
  },
  {
    deviceName: '华东海上风电',
    region: '江苏连云港',
    capacity: 120.0,
    dailyOutput: 284500,
    efficiency: 96.3,
    status: 'online',
    lastUpdate: '2024-01-15 14:32'
  },
  {
    deviceName: '西北光伏园区',
    region: '新疆哈密',
    capacity: 200.0,
    dailyOutput: 445200,
    efficiency: 92.7,
    status: 'online',
    lastUpdate: '2024-01-15 14:25'
  },
  {
    deviceName: '华南分布式光伏',
    region: '广东深圳',
    capacity: 35.2,
    dailyOutput: 78900,
    efficiency: 89.4,
    status: 'online',
    lastUpdate: '2024-01-15 14:31'
  },
  {
    deviceName: '东北生物质发电',
    region: '黑龙江哈尔滨',
    capacity: 25.0,
    dailyOutput: 52300,
    efficiency: 85.6,
    status: 'offline',
    lastUpdate: '2024-01-15 11:20'
  },
  {
    deviceName: '华中水电站',
    region: '湖北宜昌',
    capacity: 180.0,
    dailyOutput: 398600,
    efficiency: 97.1,
    status: 'online',
    lastUpdate: '2024-01-15 14:29'
  },
  {
    deviceName: '西南小水电',
    region: '四川阿坝',
    capacity: 15.5,
    dailyOutput: 32800,
    efficiency: 93.2,
    status: 'online',
    lastUpdate: '2024-01-15 14:27'
  },
  {
    deviceName: '青海光热发电',
    region: '青海海西',
    capacity: 50.0,
    dailyOutput: 108500,
    efficiency: 87.9,
    status: 'online',
    lastUpdate: '2024-01-15 14:33'
  }
])

const setFilter = (filter: string) => {
  activeFilter.value = filter
  currentPage.value = 1
}

const changePage = (page: number) => {
  currentPage.value = page
}

// 过滤和搜索数据
const filteredData = computed(() => {
  let filtered = tableData
  
  // 状态过滤
  if (activeFilter.value !== 'all') {
    filtered = filtered.filter(item => item.status === activeFilter.value)
  }
  
  // 搜索过滤
  if (searchKeyword.value) {
    filtered = filtered.filter(item => 
      item.deviceName.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      item.region.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }
  
  // 分页
  const start = (currentPage.value - 1) * pageSize
  const end = start + pageSize
  return filtered.slice(start, end)
})

// 总页数
const totalPages = computed(() => {
  let filtered = tableData
  
  if (activeFilter.value !== 'all') {
    filtered = filtered.filter(item => item.status === activeFilter.value)
  }
  
  if (searchKeyword.value) {
    filtered = filtered.filter(item => 
      item.deviceName.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      item.region.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }
  
  return Math.ceil(filtered.length / pageSize)
})

// 过滤后的总数据量
const totalFilteredCount = computed(() => {
  let filtered = tableData
  
  if (activeFilter.value !== 'all') {
    filtered = filtered.filter(item => item.status === activeFilter.value)
  }
  
  if (searchKeyword.value) {
    filtered = filtered.filter(item => 
      item.deviceName.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      item.region.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }
  
  return filtered.length
})
</script>

<style lang="less" scoped>
.center-table-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  box-sizing: border-box;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  
  .search-box {
    position: relative;
    
    .search-input {
      width: 250px;
      height: 36px;
      padding: 0 40px 0 12px;
      background: rgba(0, 30, 60, 0.6);
      border: 1px solid rgba(124, 189, 207, 0.3);
      border-radius: 18px;
      color: #ffffff;
      font-size: 14px;
      outline: none;
      
      &::placeholder {
        color: rgba(255, 255, 255, 0.5);
      }
      
      &:focus {
        border-color: #00d4ff;
        box-shadow: 0 0 8px rgba(0, 212, 255, 0.3);
      }
    }
    
    .search-icon {
      position: absolute;
      right: 12px;
      top: 50%;
      transform: translateY(-50%);
      color: #7EB7FD;
      font-size: 16px;
    }
  }
  
  .filter-buttons {
    display: flex;
    gap: 8px;
    
    .filter-btn {
      padding: 8px 16px;
      background: rgba(0, 30, 60, 0.6);
      border: 1px solid rgba(124, 189, 207, 0.3);
      border-radius: 16px;
      color: #7EB7FD;
      font-size: 14px;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: #00d4ff;
        color: #00d4ff;
      }
      
      &.active {
        background: linear-gradient(90deg, #00d4ff 0%, #0099cc 100%);
        border-color: #00d4ff;
        color: #ffffff;
      }
    }
  }
}

.table-wrapper {
  flex: 1;
  overflow-y: auto;
  border-radius: 8px;
  border: 1px solid rgba(124, 189, 207, 0.3);
}

.data-table {
  width: 100%;
  background: rgba(0, 20, 40, 0.8);
  
  :deep(.el-table__header-wrapper) {
    background: rgba(0, 30, 60, 0.9);
  }
  
  :deep(.el-table__body-wrapper) {
    background: rgba(0, 20, 40, 0.8);
  }
  
  :deep(.el-table__row) {
    transition: all 0.3s ease;
    
    &:hover {
      background: rgba(0, 212, 255, 0.1) !important;
    }
  }
  
  :deep(.el-table td),
  :deep(.el-table th) {
    padding: 12px;
    border: none;
  }
  
  :deep(.el-table--border) {
    border: none;
  }
  
  :deep(.el-table::before) {
    display: none;
  }
  
  :deep(.el-table--border::after) {
    display: none;
  }
}

.device-info {
  display: flex;
  align-items: center;
  gap: 8px;
  
  .device-icon {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    
    &.online {
      background: #52c41a;
      box-shadow: 0 0 6px rgba(82, 196, 26, 0.6);
    }
    
    &.offline {
      background: #ff4d4f;
      box-shadow: 0 0 6px rgba(255, 77, 79, 0.6);
    }
  }
}

.capacity {
  color: #00d4ff !important;
  font-weight: 600;
}

.power-output .value {
  color: #52c41a;
  font-weight: 600;
}

.efficiency {
  .efficiency-bar {
    position: relative;
    width: 80px;
    height: 20px;
    background: rgba(124, 189, 207, 0.2);
    border-radius: 10px;
    overflow: hidden;
    
    .efficiency-fill {
      height: 100%;
      background: linear-gradient(90deg, #52c41a 0%, #73d13d 100%);
      border-radius: 10px;
      transition: width 0.3s ease;
    }
    
    .efficiency-text {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: #ffffff;
      font-size: 11px;
      font-weight: 600;
    }
  }
}

.status-badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  
  &.online {
    background: rgba(82, 196, 26, 0.2);
    color: #52c41a;
    border: 1px solid rgba(82, 196, 26, 0.4);
  }
  
  &.offline {
    background: rgba(255, 77, 79, 0.2);
    color: #ff4d4f;
    border: 1px solid rgba(255, 77, 79, 0.4);
  }
}

.update-time {
  color: #7EB7FD !important;
  font-size: 12px !important;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  
  :deep(.el-pagination) {
    .el-pagination__total {
      color: #7EB7FD;
      font-size: 14px;
    }
    
    .btn-prev,
    .btn-next {
      background: rgba(0, 30, 60, 0.6);
      border: 1px solid rgba(124, 189, 207, 0.3);
      color: #7EB7FD;
      
      &:hover {
        background: rgba(0, 212, 255, 0.2);
        border-color: #00d4ff;
        color: #00d4ff;
      }
      
      &:disabled {
        background: rgba(0, 30, 60, 0.3);
        border-color: rgba(124, 189, 207, 0.2);
        color: rgba(126, 183, 253, 0.5);
      }
    }
    
    .el-pager {
      li {
        background: rgba(0, 30, 60, 0.6);
        border: 1px solid rgba(124, 189, 207, 0.3);
        color: #7EB7FD;
        margin: 0 2px;
        
        &:hover {
          background: rgba(0, 212, 255, 0.2);
          border-color: #00d4ff;
          color: #00d4ff;
        }
        
        &.is-active {
          background: linear-gradient(90deg, #00d4ff 0%, #0099cc 100%);
          border-color: #00d4ff;
          color: #ffffff;
        }
      }
    }
    
    .el-pagination__jump {
      color: #7EB7FD;
      
      .el-input__wrapper {
        background: rgba(0, 30, 60, 0.6);
        border: 1px solid rgba(124, 189, 207, 0.3);
        
        .el-input__inner {
          color: #ffffff;
          
          &::placeholder {
            color: rgba(255, 255, 255, 0.5);
          }
        }
        
        &:hover {
          border-color: #00d4ff;
        }
        
        &.is-focus {
          border-color: #00d4ff;
          box-shadow: 0 0 8px rgba(0, 212, 255, 0.3);
        }
      }
    }
  }
}
</style>